package com.ctvit.breadking.news.core.solr.config.properties;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.Objects;

/**
 * Solr配置属性类
 *
 * @author heyingcheng
 * @date 2024/10/2
 */
@Setter
@Getter
@ToString
@Component
@ConfigurationProperties("breaking.news.tester.solr")
public class SolrProperties implements InitializingBean {

    private String username;

    private String password;

    private int connTimeout;

    private int socketTimeout;

    private String shardsUri;

    private String[] baseUrls;

    @Override
    public void afterPropertiesSet() throws Exception {
        Assert.hasLength(this.username, "必须在配置文件中指定username。");
        Assert.hasLength(this.password, "必须在配置文件中指定password。");
        Assert.state(this.connTimeout > 0, "必须在配置文件中指定connTimeout。");
        Assert.state(this.socketTimeout > 0, "必须在配置文件中指定socketTimeout。");
        Assert.hasLength(this.shardsUri, "必须在配置文件中指定shardsUri。");
        Assert.notNull(this.baseUrls, "必须在配置文件中指定baseUrls。");
    }

    public void setBaseUrls(String[] baseUrls) {
        if (Objects.nonNull(baseUrls)) {
            this.baseUrls = baseUrls.clone();
        }
    }

    public String[] getBaseUrls() {
        if (Objects.nonNull(baseUrls)) {
            return baseUrls.clone();
        }
        return new String[0];
    }

}
